package com.sy.service.impl;

import com.sy.service.VerifyCodeService;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

/**
 * @className: VerifyCodeServiceImpl
 * @author: Mr.Liu
 * @date: 2025/11/10 14:04
 * @Version: 1.0
 * @description:
 */
@Service
@ConfigurationProperties(prefix = "hywx")
@Slf4j
@Data
public class VerifyCodeServiceImpl implements VerifyCodeService {
    private String url;
    private String apiId;
    private String apiKey;
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public void sendMsg(String phone) {
        log.info("apiID:{},apiKey:{},url:{}",apiId,apiKey,url);
        //生成验证码
        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);
        String content = new String("您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。");
        System.out.println(content);
        //将验证码存储到redis中-->设置失效时间(3分钟)
        redisTemplate.opsForHash().put("code",phone,mobile_code+"");
        redisTemplate.expire("code",3, TimeUnit.MINUTES);
    }
//    @Override
    public void sendMsg1(String phone) {
        log.info("apiID:{},apiKey:{},url:{}",apiId,apiKey,url);
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");
        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);
        String content = new String("您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。");
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", apiId), //查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
                new NameValuePair("password", apiKey), //查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", phone),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);
            String SubmitResult = method.getResponseBodyAsString();
            //System.out.println(SubmitResult);
            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();
            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");
            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);
            if ("2".equals(code)) {
                //将验证码存储到redis中-->设置失效时间(3分钟)
                redisTemplate.opsForHash().put("code",phone,mobile_code+"");
                redisTemplate.expire("code",3, TimeUnit.MINUTES);
                System.out.println("短信提交成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
